﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// PublicClass 的摘要说明
/// </summary>
public class PublicClass
{
    public PublicClass()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region 创建一个指定列名的数据表；
    /// <summary>
    /// 创建一个指定列名的数据表；
    /// </summary>
    /// <param name="StrColName">指定的列名</param>
    /// <returns>返回创建的列名</returns>
    public static DataTable BuildDataTableCol(string[] StrColName)
    {  
        DataTable dt = new DataTable();
        DataColumn column = null;
        foreach (string ColName in StrColName)
        {
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = ColName;
            column.AutoIncrement = false;
            column.Caption = ColName;
            column.ReadOnly = false;
            column.Unique = false;
            dt.Columns.Add(column);
        }
        return dt;
    }
    #endregion
    #region 将表格的指定列导出到Excel文件
    #region
    /// <summary>
    /// 将表格的指定列导出到Excel文件
    /// </summary>
    /// <param name="dt">数据表</param>
    /// <param name="col">需导出的列</param>
    /// <param name="colname">显示列名</param>
    /// <returns></returns>
    //public void outputToExcel(System.Data.DataTable dt, string col, string colname,
    //    ref Microsoft.Office.Interop.Excel.Workbook myworkbook, 
    //    Microsoft.Office.Interop.Excel.Worksheet myworksheet)
    //{
        
    //    string[] cols = col.Split(',');
    //    string[] names = colname.Split(',');
    //    for (int i = 0; i < names.Length; i++)
    //    {
    //        myworksheet.Cells[1, i + 1] = names[i];
    //    }
    //    if (dt != null)
    //    {
    //        int rowindex = 2;
    //        foreach (DataRow dr in dt.Rows)
    //        {
    //            for (int i = 0; i < cols.Length; i++)
    //            {
    //                myworksheet.Cells[rowindex, i + 1] = dr[cols[i]].ToString();
    //            }
    //            rowindex++;
    //        }
    //    }
    //    //不可见，即后台处理
        
        
    //    //myworkbook.Post();
    //    //myworkbook.ReloadAs(Microsoft.Office.MsoEncoding.msoEncodingWestern);

    //    //保存刚才创建的Excel表格
    //    //DateTime dtime = DateTime.Now;
    //    //string strtmp = "未定结婚日期" + dtime.ToShortDateString().Replace("-", "") +
    //    //    dtime.ToFileTime().ToString() + ".xls";
    //    //string urlstr = "outfile/" + strtmp;
    //    //string strFileName = MapPath("outfile\\" + strtmp);
    //    //myworkbook.Save();
    //    //myworkbook.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    //    //        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value,
    //    //        Missing.Value, Missing.Value, Missing.Value);
    //    //myexcel.Application.Workbooks.Close();
    //    //myexcel.Application.Quit();
    //    //myexcel.Quit();
    //    ////释放对象
    //    //System.Runtime.InteropServices.Marshal.ReleaseComObject(myexcel);
    //    //GC.Collect();//调用垃圾收集内存
    //    //return strReturn;
    //}
    #endregion

    public string printToExcel(DataTable dt,string col,string colname,bool isCreateHead)
    {
        string[] cols = col.Split(',');

        string strResult = "<table border=1>";
        //表头
        if (isCreateHead)
        {
            string[] names = colname.Split(',');
            if (names.Length > 0)
            {
                strResult += "<tr>";
                for (int i = 0; i < names.Length; i++)
                {
                    strResult += "<td>" + names[i] + "</td>";
                }
                strResult += "</tr>";
            }
        }
        else
        {
            strResult += colname;
        }
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strResult += "<tr>";
            for (int j = 0; j < cols.Length; j++)
            {
                strResult += "<td>" + dt.Rows[i][cols[j]] + "</td>";
            }
            strResult += "</tr>";
        }
        strResult += "</table>";

        return strResult;
    }
    
    #endregion
}
